package com.lcj.eduService.controller;


import com.lcj.commonutils.R;
import com.lcj.eduService.entity.EduChapter;
import com.lcj.eduService.entity.chapter.ChapterVo;
import com.lcj.eduService.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-14
 */
@Api(description="课程章节管理")
@RestController
@RequestMapping("/eduService/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;


    //课程大纲列表
    @ApiOperation(value = "嵌套章节数据列表")
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    @ApiOperation(value = "新增章节")
    //添加课程章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询章节")
    //根据id查询课程章节
    @GetMapping("getChapterInfo/{chapterId}")
    public R  getChapterInfo (@PathVariable String chapterId){
        EduChapter eduChapter =eduChapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }

    //修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    //删除方法
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean result = eduChapterService.deleteChapter(chapterId);
        if(result){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }
    }
}

